﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using AddDataToDatabaseWebApplication.InsertDataDataSetTableAdapters;
using System.Threading;

namespace AddDataToDatabaseWebApplication
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "InsertDataToDatabaseService" in code, svc and config file together.
    public class InsertDataToDatabaseService : IInsertDataToDatabaseService
    {
        private ManageQuesSetTableAdapter mqsta = new ManageQuesSetTableAdapter();
        private ManageQuestionTableAdapter mqta = new ManageQuestionTableAdapter();
        private QuestionNameTableAdapter qnta = new QuestionNameTableAdapter();
        private QuestionSetNameTableAdapter qsnta = new QuestionSetNameTableAdapter();
        private ManageDefaultAnswerTableAdapter mdata = new ManageDefaultAnswerTableAdapter();

        public string InsertAnTextQuestion(string adminId, string questionSetName, string questionName)
        {
            try
            {
                int questionSetId = InsertQuestionSet(questionSetName, questionName);
                int questionId = InsertQuestion(questionName);
                mqsta.Insert(questionSetId, Convert.ToInt16(adminId));
                mqta.Insert(questionId, questionSetId);
                mdata.Insert("", questionId);
                return "success";
            }
            catch (Exception)
            {
                return "Failed";
            }
        }



        private int InsertQuestion(string questionName)
        {
            int questionId = -1;
            qnta.Insert(questionName);
            questionId = (int)qnta.SelectTheLargestIdFromQuestionNameTable(); ;
            return questionId;
        }

        private int InsertQuestionSet(string questionSetName, string questionName)
        {
            int questionSetId = -1;
            qsnta.Insert(questionSetName);
            questionSetId = (int)qsnta.SelectTheLargestIdFromQuestionSetNameTable();
            return questionSetId;
        }

        public string InsertAnMCQuestion(string adminId, string questionSetName, string questionName, string choice1, string choice2, string choice3, string choice4, string choice5)
        {
            try
            {
                int questionSetId = InsertQuestionSet(questionSetName, questionName);
                int questionId = InsertQuestion(questionName);
                mqta.Insert(questionId, questionSetId);
                List<string> templist = new List<string>();
                templist.Add(choice1);
                templist.Add(choice2);
                templist.Add(choice3);
                templist.Add(choice4);
                templist.Add(choice5);

                for (int i = 0; i < 5; i++)
                {
                    mdata.Insert(templist[i], questionId);
                }
                mqsta.Insert(questionSetId, Convert.ToInt16(adminId));
                return "success";
            }
            catch (Exception)
            {

                return "Failed";
            }
        }


    }
}
